﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Configuration;

using Analysis.Entities;
using Analysis.DAL;
using Analysis.DAL.Repositories;
using Analysis.Code;

namespace Analysis
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetActiveLink();

            if (!IsPostBack)
            {
                if (Roles.IsUserInRole("admin"))
                    lblUserName.Text = "Администратор";
                else
                {
                    using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
                    {
                        User user = repo.Find(Membership.GetUser().UserName);
                        lblUserName.Text = user.FullName;

                        lihlReport.Visible = user.IsPageReport;
                        lihlAP.Visible = user.IsPageAddress;
                        lihlAssort.Visible = user.IsPageAssort;
                        lihlMML.Visible = user.IsPageMML;
                        lihlSale.Visible = user.IsPageSale;
                        lihlUser.Visible = user.IsPageUser;
                        lihlSetup.Visible = user.IsPageSetup;
                    } 
                }
            }
        }

        protected void lbLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Session.Abandon();

            // clear authentication cookie
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            // clear session cookie (recommend)
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie2);

            FormsAuthentication.RedirectToLoginPage();
        }

        private void SetActiveLink()
        {
            string namePage = Page.GetType().FullName;

            tophlUser.Attributes["class"] = "user_management_nav";
            tophlDashboard.Attributes["class"] = "dashboard_nav";
            tophlSetup.Attributes["class"] = "dashboard_nav";
            tophlAP.Attributes["class"] = "contracts_nav";
            tophlAssort.Attributes["class"] = "contacts_nav";
            tophlSale.Attributes["class"] = "contacts_nav";
            tophlMML.Attributes["class"] = "dashboard_nav";
            
            if (namePage == "ASP.usermanagement_aspx")
            {
                tophlUser.Attributes["class"] += " active active_user_management";
            }
            else if (namePage == "ASP.dashboard_aspx")
            {
                tophlDashboard.Attributes["class"] += " active active_dashboard";
            }
            else if (namePage == "ASP.setup_aspx")
            {
                tophlSetup.Attributes["class"] += " active active_dashboard";
            }
            else if (namePage == "ASP.ap_aspx")
            {
                tophlAP.Attributes["class"] += " active active_contracts";
            }
            else if (namePage == "ASP.scu_aspx")
            {
                tophlAssort.Attributes["class"] += " active active_contacts";
            }
            else if (namePage == "ASP.sale_aspx")
            {
                tophlSale.Attributes["class"] += " active active_contacts";
            }
            else if (namePage == "ASP.targetscu_aspx")
            {
                tophlMML.Attributes["class"] += " active active_dashboard";
            }
        }
    }
}